import api from 'api'
import store from 'store'
import { Toast } from 'vant'
import Vue from 'vue'

Vue.use(Toast)

export default{
  namespaced:true,
  state:{
    footerShow:true,
    courseid:"",
    storeData:{
      club_info:{}
    },
    mealList:[],
    coursePic:{
      coursePicList:[],
      coursePicTotal:[]
    },
    ifAddPadding:false,
    showBottom:true,
    orderFromType:-1, 
    is_first:1 //首次访问
  },
  mutations:{
    saveStore(state,data){
      state.storeData = data
    },
    setOrderFromType(state,type){
      state.orderFromType = type
    },
    setAddPadding(state,params){
      state.ifAddPadding = params
    },
    setShowBottom(state,params){
      state.showBottom = params
    }
  },
  actions:{
    init(context){ //刷新时初始化
      let courseid = sessionStorage.getItem("courseid")
      if(courseid){
        context.dispatch("getStoreData",courseid)
        let yuding_teetime = JSON.parse(sessionStorage.getItem("yuding_teetime")) 
        let meal_teetime = JSON.parse(sessionStorage.getItem("meal_teetime")) 
        let meal_id = sessionStorage.getItem("meal_id")
        if(meal_id){
          store.dispatch("meal/getMealDetail",meal_id).then(()=>{
            if(meal_teetime){
              store.commit('meal/setData',meal_teetime)
            }
          })
        }
        if(yuding_teetime){
          store.commit("yuding/setData",yuding_teetime)
        }
      }else{
        // 刷新时session没有courseid,则要求再次扫码进入
      }
      store.commit("user/save_userInfo")
    },
    getStoreData(context,courseid){ //获取球场信息
      return new Promise((resolve, reject) => {
        context.state.courseid = courseid
        api.get("/course/courseinfo",{courseid}).then(e=>{
          context.state.storeData = e.data
          sessionStorage.setItem("courseid",e.data.id) // 保存球场id
          let time = {
            starttime:e.data.starttime,
            endtime:e.data.endtime
          }
          store.commit("yuding/saveTime",time) // 配置球场营业时间
          context.dispatch("getMeal")
          context.dispatch("getcourseAlbum")
          resolve()
        })
      });
    },
    getMeal(context){ //获取首页套餐列表
      let params = {
        courseid:context.state.courseid,
        page:1,
        pagesize:3
      }
      api.get("/course/courseProductList",params).then(e=>{
        context.state.mealList = e.data.products
      })  
    },
    getcourseAlbum(context){ //获取首页图集
      let courseid = context.state.courseid
      api.get("/course/courseAlbumWap",{courseid}).then(e=>{
        context.state.coursePic.coursePicList = e.data
        for(let i of e.data){
          for(let j of i.pics){
            context.state.coursePic.coursePicTotal.push(j.pic)
          }
        }
      })
    }
  }
}